﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/Disposable.cs
//  *
//  ***************************************************************************

#endregion

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Haplous.Core
{
    /// <summary>
    ///     A base class for objects that implement the <see cref="IDisposable" /> interface.
    /// </summary>
    [DebuggerDisplay("Disposable: IsDisposed={IsDispose}")]
    public abstract class Disposable : IDisposable
    {
        /// <summary>
        ///     The value of the <see cref="_disposed" /> field after the <see cref="IDisposable.Dispose()" /> method has been
        ///     called.
        /// </summary>
        private const int Disposed = 1;

        /// <summary>
        ///     The value of the <see cref="_disposed" /> field before the <see cref="IDisposable.Dispose()" /> method has been
        ///     called.
        /// </summary>
        private const int NotDisposed = 0;

        /// <summary>
        ///     A flag indicating whether or not the <see cref="IDisposable.Dispose()" /> method has been called.
        /// </summary>
        private int _disposed = NotDisposed;

        /// <summary>
        ///     Gets a value indicating whether the <see cref="IDisposable.Dispose()" /> method has been called on this instance.
        /// </summary>
        /// <value><see langword="true" /> if this instance is disposed; otherwise, <see langword="false" />.</value>
        public bool IsDisposed
        {
            get { return _disposed != NotDisposed; }
        }

        #region IDisposable Members

        /// <summary>
        ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
        public void Dispose()
        {
            // Only dispose once.
            if(Interlocked.CompareExchange(ref _disposed, Disposed, NotDisposed) == Disposed)
                return;

            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        /// <summary>
        ///     Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing">
        ///     <see langword="true" /> to release both managed and unmanaged resources;
        ///     <see langword="false" /> to release only unmanaged resources.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {}

        /// <summary>
        ///     Checks the <see cref="IsDisposed" /> property and throws a <see cref="ObjectDisposedException" /> exception
        ///     if it is.  Used on entrance to functions to prevent access after inheriting objects have been disposed of.
        /// </summary>
        /// <exception cref="System.ObjectDisposedException">Object cannot be accessed after 'Dispose()' has been called.</exception>
        protected void EnforceNotDisposed()
        {
            if(_disposed != NotDisposed)
                throw new ObjectDisposedException("Object cannot be accessed after 'Dispose()' has been called.");
        }
    }
}